from django import template  
register = template.Library()  

@register.filter("tnij_znaki")  
def tnij_znaki(value, max_length):  
    if len(value) > max_length:  
        truncd_val = value[:max_length]  
        if value[max_length+1] != " ":  
            truncd_val = truncd_val[:truncd_val.rfind(" ")]  
        return  truncd_val + "..."
    return value

@register.filter("dataczas")  
def dataczas(data):
    if data is not "":
	from datetime import datetime
	teraz = datetime.now()
	roznica = teraz - data
	if roznica.days == 0:
	    return "dzisiaj %s"%datetime.strftime(data, '%H:%M:%S')
	if roznica.days == 1:
	    return "wczoraj %s"%datetime.strftime(data, '%H:%M:%S')
	return datetime.strftime(data, '%d-%m-%y %H:%M:%S')

@register.filter("data")  
def data(data):
    if data is not "":
	from datetime import date
	teraz = date.today()
	roznica = teraz - data
	if roznica.days == 0:
	    return "dzisiaj"
	if roznica.days == 1:
	    return "wczoraj"
	return date.strftime(data, '%d-%m-%y')